package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/v1/file/")
@Tag(name="文件上传模块")
public class UploadController {
    @PostMapping("upload")
    @Operation(summary = "上传文件功能")
    @ApiOperationSupport(order = 10)
    public JsonResult upload(MultipartFile file) throws IOException {
        //终极目标 把文件保存到服务器的指定目录中[d://files]中
        //1.原始文件名fileName =meinv.png
        String filename = file.getOriginalFilename();
        //2.获取原始文件名的后缀.png
        String suffix=filename.substring(filename.lastIndexOf("."));
        //3.生成全球唯一UUID[处理文件名]
        UUID uuid = UUID.randomUUID();
        //4.拼接文件名
        filename = uuid+suffix;
        //**********************二 处理路径***************************
        //1.自定义日期 /yyyy/MM/dd
        SimpleDateFormat sdf=new SimpleDateFormat("/yyyy/MM/dd/");
       String datePath = sdf.format(new Date());
       //2.定义静态资源路径PATH
        String dirPath = "d:/files";
        //3.创建目录d:/files/2025/07/14
        File dirFile=new File(dirPath+datePath);
        if(!dirFile.exists()){
            dirFile.mkdirs();
        }
        //**********************三 保存文件***************************
        String filePath = dirPath+datePath+filename;
        file.transferTo(new File(filePath));
        //服务器端把图片路径返回给客户端
        return JsonResult.ok(datePath+filename);
    }
}
